package com.example.androidexample.settings;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.androidexample.R;
import com.example.basic.base.BaseActivity;
import com.example.basic.utils.IntentUtils;

public class SettingsActivity extends BaseActivity implements PreferenceFragment.OnPreferenceStartFragmentCallback  {

    private static final String TAG = "SettingsActivity";
    private ConstraintLayout sToolbar;

    static final String EXTRA_FRAGMENT = "extra_fragment";
    static final String EXTRA_FRAGMENT_ARGUMENTS = "extra_fragment_args";

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        Intent intent = getIntent();
        String initialFragment = intent.getStringExtra(EXTRA_FRAGMENT);
        Bundle initialArguments = intent.getBundleExtra(EXTRA_FRAGMENT_ARGUMENTS);

//        if (savedInstanceState == null) {
//            if (initialFragment == null) initialFragment = MainPreferences.class.getName();
//            Fragment fragment = Fragment.instantiate(this, initialFragment, initialArguments);
//            getFragmentManager().beginTransaction()
//                    .replace(android.R.id.content, fragment)
//                    .commit();
//        }

        initToolBar();
    }

    private void initToolBar() {
        LinearLayout actionBarRoot = findViewById(R.id.action_bar_root);

        ConstraintLayout toolbar = (ConstraintLayout) View.inflate(this, R.layout.base_tool_bar, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        actionBarRoot.addView(toolbar, 0, lp);
        this.sToolbar = toolbar;

        findViewById(R.id.ic_back).setOnClickListener(v -> onBackPressed());
    }

    public static void startPage(Context context,
                                 @Nullable Class<? extends Fragment> fragment,
                                 @Nullable String args) {
        String fragmentName = fragment != null ? fragment.getName() : null;

        Intent intent = new Intent();
        intent.setClass(context, SettingsActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        if (!TextUtils.isEmpty(fragmentName)) {
            intent.putExtra(EXTRA_FRAGMENT, fragmentName);
        } else {
            throw new RuntimeException("should set fragment first!");
        }

        if (!TextUtils.isEmpty(args)) {
            intent.putExtra(EXTRA_FRAGMENT_ARGUMENTS, args);
        }

        IntentUtils.safeStartActivity(context, intent);
    }

    @Override
    public void setTitle(int titleId) {
        if (sToolbar != null) {
            TextView tvTitle = sToolbar.findViewById(R.id.tool_bar_title);
            tvTitle.setText(getString(titleId));
        } else {
            super.setTitle(titleId);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        if (sToolbar != null) {
            TextView tvTitle = sToolbar.findViewById(R.id.tool_bar_title);
            tvTitle.setText(title);
        } else {
            super.setTitle(title);
        }
    }

    @Override
    protected int setLayoutXmlid() {
        return 0;
    }

    @Override
    public String getTag() {
        return TAG;
    }

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
        // start fragment
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClass(this, getClass());
        intent.putExtra(EXTRA_FRAGMENT, pref.getFragment());
        intent.putExtra(EXTRA_FRAGMENT_ARGUMENTS, pref.getExtras());
        startActivity(intent);

        return true;
    }
}
